﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace Velsto
{
    public partial class FormContour : Form
    {
        ContourDelegate contDel;
        int contNrInt;
        double contMin;
        double contMax;

        public FormContour()
        {
            InitializeComponent();
        }

        public FormContour(ContourDelegate cd, int contNrInt2, double contMin2, double contMax2)
        {
            InitializeComponent();
            contDel = cd;
            contNrInt = contNrInt2;
            contMin = contMin2;
            contMax = contMax2;
            texb_int.Text = contNrInt.ToString();
            texb_min.Text = contMin.ToString("0.0", CultureInfo.InvariantCulture);
            texb_max.Text = contMax.ToString("0.0", CultureInfo.InvariantCulture);
        }
        
        private void bu_close_Click(object sender, EventArgs e)
        {
            this.Close();
        } 

        private void bu_apply_Click(object sender, EventArgs e)
        {
            if (contMax <= contMin)
            {
                MessageBox.Show("**ERROR** Maximum must be greater then Minimum");
            }
            else if (contNrInt < 1)
            {
                MessageBox.Show("**ERROR** Nr of Intervals must be > 0");
            }
            else
            {
                contDel(contNrInt, contMin, contMax);
            }
        }

        private void texb_int_Validating(object sender, CancelEventArgs e)
        {
            int newInt = 0;
            if ((!int.TryParse(texb_int.Text,
                              out newInt) || newInt < 0)
                           )
            {
                MessageBox.Show("NrIntervals must be integer");
                e.Cancel = true;
            }
        }

        private void texb_int_Validated(object sender, EventArgs e)
        {
            contNrInt = Convert.ToInt32(texb_int.Text);
        }
        
        private void texb_min_Validating(object sender, CancelEventArgs e)
        {
            double newDouble;
            if (!double.TryParse(texb_min.Text,
                out newDouble)
               )
            {
                e.Cancel = true;
                MessageBox.Show("Minimum must be numeric");
            }
        }
        
        private void texb_min_Validated(object sender, EventArgs e)
        {         
            contMin = parseDoubleString(texb_min.Text.Replace(',','.'));
        }
        
        private void texb_max_Validating(object sender, CancelEventArgs e)
        {
            double newDouble;
            if (!double.TryParse(texb_max.Text,
                out newDouble)
               )
            {
                e.Cancel = true;
                MessageBox.Show("Maximum must be numeric");
            }
        }
        
        private void texb_max_Validated(object sender, EventArgs e)
        {
            contMax = parseDoubleString(texb_max.Text.Replace(',', '.'));
        }

        // Method to parse with both decimal separators
        public double parseDoubleString(string sd)
        {
            var currentCulture = System.Globalization.CultureInfo.InstalledUICulture;
            var numberFormat = (System.Globalization.NumberFormatInfo)
            currentCulture.NumberFormat.Clone();
            numberFormat.NumberDecimalSeparator = ".";
            double number;
            bool succeeded = double.TryParse(sd, System.Globalization.NumberStyles.Any, numberFormat, out number);
            if (succeeded)
            {
                return number;
            }
            else
            {
                numberFormat.NumberDecimalSeparator = ",";
                bool succeeded2 = double.TryParse(sd, System.Globalization.NumberStyles.Any, numberFormat, out number);
                if (succeeded2)
                {
                    return number;
                }
                else
                {
                    return -999.9;
                }
            }
        }
   
    }
 
}
